package connotation.utils

import android.content.Context
import android.os.Environment
import android.util.Log
import com.zyf.connotation.utils.ShardUtil
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream


/**
 * 作者：Think
 * 创建于 2017/6/3 13:18
 */
object AssetsUtils {
    var dir = Environment.getExternalStorageDirectory().path +"/Connotation/skin"

    fun CopyAssetsSkinFile(context: Context): Boolean {
        Log.e("path", dir)
        var file = File(dir)
        if(!file.exists()){
            file.mkdirs()
        }
        if(ShardUtil.getPreferenceBoolean(context,"skin")){
            return true
        }
        val assetManager = context.assets
        assetManager.list("skin").forEach {
            Log.e("skin",it)
            var inStream: InputStream? = null
            var outStream: OutputStream? = null
            try {
                inStream = assetManager.open("skin/"+it)
                val newFileName =File(file,it)
                outStream = FileOutputStream(newFileName)
                var buffer = ByteArray(1024)
                var read:Int
                while (true) {
                    read = inStream.read(buffer)
                    if(read == -1 ){
                        break
                    }
                    outStream?.write(buffer, 0, read)
                }
                inStream.close()
                inStream = null
                outStream?.flush()
                outStream?.close()
                outStream = null
                ShardUtil.savePreferenceBoolean(context,"skin",true)
            } catch (e: Exception) {
                e.printStackTrace()
                ShardUtil.savePreferenceBoolean(context,"skin",false)
            }
        }
        return true
    }
}